package com.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.studay.bean.ChannelInfo;
import com.studay.bean.Course;
import com.studay.bean.DianBoPojo;
import com.util.EducationUtil;

public class DiaoBoHandler extends DefaultHandler {

	private Map<String, String> map;
	private ArrayList<DianBoPojo> list;
	private DianBoPojo dbp;
	private StringBuilder builder;
	private ArrayList<Course> dbcpList;
	private Course dbcp;
	private ChannelInfo channelInfo;

	public DiaoBoHandler() {
		super();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		builder.append(ch, start, length);
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		super.endElement(uri, localName, name);
		if (localName.equals("project")) {
			dbp.setChildList(dbcpList);
			list.add(dbp);
		} else if (localName.equals("course")) {
			dbcpList.add(dbcp);
		} else if (localName.equals("filehash")) {
			StringTokenizer st = new StringTokenizer(builder.toString(), "\n");
			if (st.hasMoreTokens())
				dbcp.setFilehash(st.nextToken().trim());
			else
				dbcp.setFilehash("");
		} else if (localName.equals("playlength")) {
			StringTokenizer st = new StringTokenizer(builder.toString(), "\n");
			if (st.hasMoreTokens())
				dbcp.setPlaylength(EducationUtil.stringToInt(st.nextToken()
						.trim()));
			else
				dbcp.setPlaylength(0);
		} else if (localName.equals("teacher")) {
			StringTokenizer st = new StringTokenizer(builder.toString(), "\n");
			if (st.hasMoreTokens())
				dbcp.setTeacher(st.nextToken().trim());
			else
				dbcp.setTeacher("");
		} else if (localName.equals("teacherinfo")) {
			StringTokenizer st = new StringTokenizer(builder.toString(), "\n");
			if (st.hasMoreTokens())
				dbcp.setTeacherInfo(st.nextToken().trim());
			else
				dbcp.setTeacherInfo("");
		} else if (localName.equals("project")) {
			StringTokenizer st = new StringTokenizer(builder.toString(), "\n");
			if (st.hasMoreTokens())
				dbcp.setProject(st.nextToken().trim());
			else
				dbcp.setProject("");
		}
		builder.setLength(0);
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		list = new ArrayList<DianBoPojo>();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		map = new HashMap<String, String>();
		// 保存属性
		for (int i = 0; i < attributes.getLength(); i++) {
			map.put(attributes.getQName(i), attributes.getValue(attributes
					.getQName(i)));
		}
		if (localName.equals("classxmlfile")) {
			channelInfo = new ChannelInfo();
			channelInfo.setFisserverurl(map.get("fisserverurl").toString());
			channelInfo.setNodes(map.get("nodes").toString());
			channelInfo.setProtocl(map.get("protocl").toString());
			channelInfo.setVersion(map.get("version").toString());
		}

		if (localName.equals("project")) {
			dbp = new DianBoPojo();
			dbp.setName(map.get("name").toString());
			dbp.setRemark(map.get("remark").toString());
			dbcpList = new ArrayList<Course>();
		} else if (localName.equals("course")) {
			dbcp = new Course();
			dbcp.setName(map.get("name").toString());
		}
	}

	public ArrayList<DianBoPojo> getList() {
		return list;
	}

	public void setList(ArrayList<DianBoPojo> list) {
		this.list = list;
	}

	public ChannelInfo getChannelInfo() {
		return channelInfo;
	}

	public void setChannelInfo(ChannelInfo channelInfo) {
		this.channelInfo = channelInfo;
	}
}